home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / tde31.zip / PORT.C < prev    next >
C/C++ Source or Header  |  1993-08-29  |  9KB  |  270 lines

  1. /*
  2.  * Now that I own both MSC 7.0 and BC 3.1, lets rearrange stuff so both
  3.  * compilers can compile TDE.  Several implementation specific functions
  4.  * needed for both compilers were gathered into this file.
  5.  *
  6.  * Incidentally, there is difference between a NULL line pointer and
  7.  * a pointer to a line that contains no characters.  For example, calling
  8.  *
  9.  *       line = malloc( 0 );
  10.  *
  11.  *                   or, more precisely in TDE:
  12.  *
  13.  *       line = _fmalloc( 0 );
  14.  *       line = farmalloc( 0 );
  15.  *
  16.  * will return a valid pointer to an item of 0 length in some compilers
  17.  * and a NULL pointer in other compilers.  malloc( 0 ) will return a valid
  18.  * pointer to an object of zero length in MSC.  malloc( 0 ) will return a
  19.  * NULL pointer in BC.  The problem with returning a NULL pointer for
  20.  * malloc( 0 ) is that it's a little harder to tell if the heap is out of
  21.  * memory or if we have a valid NULL pointer.  On the other hand, the good
  22.  * part about returning a NULL pointer for malloc( 0 ) is that extra space
  23.  * is not wasted for an object of 0 length.  In TDE, we will test for 0
  24.  * before calling my_malloc( ) and set an ERROR code if out of memory.
  25.  *
  26.  * Although many PC C compilers have findfirst and findnext functions for
  27.  * finding files, let's write our own to keep a closer watch on
  28.  * critical errors.
  29.  *
  30.  *
  31.  * New editor name:  TDE, the Thomson-Davis Editor.
  32.  * Author:           Frank Davis
  33.  * Date:             June 5, 1991, version 1.0
  34.  * Date:             July 29, 1991, version 1.1
  35.  * Date:             October 5, 1991, version 1.2
  36.  * Date:             January 20, 1992, version 1.3
  37.  * Date:             February 17, 1992, version 1.4
  38.  * Date:             April 1, 1992, version 1.5
  39.  * Date:             June 5, 1992, version 2.0
  40.  * Date:             October 31, 1992, version 2.1
  41.  * Date:             April 1, 1993, version 2.2
  42.  * Date:             June 5, 1993, version 3.0
  43.  * Date:             August 29, 1993, version 3.1
  44.  *
  45.  * This code is released into the public domain, Frank Davis.
  46.  * You may use and distribute it freely.
  47.  */
  48.  
  49. #include "tdestr.h"
  50. #include "common.h"
  51. #include "tdefunc.h"
  52. #include "define.h"
  53.  
  54.  
  55. /*
  56.  * Name:    my_malloc
  57.  * Purpose: malloc from the far heap
  58.  * Date:    April 1, 1993
  59.  * Passed:  mem:  pointer to memory to free in far heap
  60.  *          rc:   pointer to return code
  61.  * Notes:   set the return code only if an ERROR occured with malloc.
  62.  *           returning a NULL pointer is not neccessarily an ERROR.
  63.  */
  64. void far * my_malloc( size_t size, int *rc )
  65. {
  66. void far *mem;
  67.  
  68.    assert( size < MAX_LINE_LENGTH );
  69.  
  70.    if (size == 0)
  71.  
  72.       /*
  73.        * if 0 bytes are requested, return NULL
  74.        */
  75.       mem = NULL;
  76.    else {
  77.  
  78. #if defined( __MSC__ )
  79.       mem = _fmalloc( size );
  80. #else
  81.       mem = farmalloc( size );
  82. #endif
  83.  
  84.       /*
  85.        * if malloc failed, return NULL and an ERROR.
  86.        */
  87.       if (mem == NULL)
  88.          *rc = ERROR;
  89.    }
  90.    return( mem );
  91. }
  92.  
  93.  
  94. /*
  95.  * Name:    my_free
  96.  * Purpose: free memory from the far heap
  97.  * Date:    April 1, 1993
  98.  * Passed:  mem:  pointer to memory to free in far heap
  99.  */
  100. void my_free( void far *mem )
  101. {
  102.    assert( mem != NULL );
  103.  
  104. #if defined( __MSC__ )
  105.    _ffree( mem );
  106. #else
  107.    farfree( mem );
  108. #endif
  109. }
  110.  
  111.  
  112. /*
  113.  * Name:    my_findfirst
  114.  * Purpose: find the first file matching a pattern using DOS interrupt
  115.  * Date:    January 6, 1992
  116.  * Passed:  dta:    disk transfer address
  117.  *          path:   path to search for files
  118.  *          f_attr: attributes of files to search for
  119.  * Notes:   return codes for my_findfirst:
  120.  *             0  no error
  121.  *             2  file is invalid or does not exist
  122.  *             3  path is invalid or does not exist
  123.  *            18  no matching directory entry was found
  124.  *            -1  check the critical error flag for critical errors
  125.  */
  126. int  my_findfirst( DTA far *dta, char far *path, int f_attr )
  127. {
  128. void far *old_dta;
  129. void far *new_dta;
  130. int  rc;
  131.  
  132.    new_dta = (void far *)dta;
  133.  
  134.    ASSEMBLE {
  135.  
  136. /*
  137. ; save the old dta
  138. */
  139.         mov     ah, 0x2f                /* DOS get dta */
  140.         int     0x21                    /* DOS interrupt */
  141.         mov     WORD PTR old_dta, bx    /* save OFFSET of old DTA */
  142.         mov     ax, es
  143.         mov     WORD PTR old_dta+2, ax  /* save SEGMENT of old DTA */
  144.  
  145. /*
  146. ; set the new dta
  147. */
  148.         push    ds                      /* save ds */
  149.         mov     dx, WORD PTR new_dta    /* get OFFSET of new dta */
  150.         mov     ax, WORD PTR new_dta+2  /* get SEGMENT of new dta */
  151.         mov     ds, ax                  /* put it in ds */
  152.         mov     ah, 0x1a                /* DOS set dta */
  153.         int     0x21                    /* DOS interrupt */
  154.         pop     ds                      /* get back ds */
  155.  
  156. /*
  157. ; find first matching file
  158. */
  159.         push    ds                      /* save ds */
  160.         mov     cx, WORD PTR f_attr     /* file attributes to search for */
  161.         mov     dx, WORD PTR path       /* get OFFSET of path */
  162.         mov     ax, WORD PTR path+2     /* get SEGMENT of path */
  163.         mov     ds, ax                  /* put it in ds */
  164.         mov     ah, 0x4e                /* DOS find first file */
  165.         int     0x21                    /* DOS interrupt */
  166.         pop     ds                      /* get back ds */
  167.  
  168. /*
  169. ; save the return code
  170. */
  171.         jc      an_error                /* carry is set if an error occured */
  172.         xor     ax, ax                  /* zero out ax, return OK if no error */
  173.    }
  174. an_error:
  175.  
  176.    ASSEMBLE {
  177.         mov     WORD PTR rc, ax         /* save the return code */
  178.  
  179. /*
  180. ; get back old dta
  181. */
  182.         push    ds                      /* save ds */
  183.         mov     dx, WORD PTR old_dta    /* get OFFSET of old dta */
  184.         mov     ax, WORD PTR old_dta+2  /* get SEGMENT of old dta */
  185.         mov     ds, ax                  /* put it in ds */
  186.         mov     ah, 0x1a                /* DOS set dta */
  187.         int     0x21                    /* DOS interrupt */
  188.         pop     ds                      /* get back ds */
  189.    }
  190.    if (ceh.flag == ERROR)
  191.       rc = ERROR;
  192.    return( rc );
  193. }
  194.  
  195.  
  196. /*
  197.  * Name:    my_findnext
  198.  * Purpose: find the next file matching a pattern using DOS interrupt
  199.  * Date:    January 6, 1992
  200.  * Passed:  dta:  disk transfer address
  201.  * Notes:   my_findfirst() MUST be called before calling this function.
  202.  *          return codes for my_findnext (see DOS tech ref manuals):
  203.  *             0  no error
  204.  *             2  path is invalid or does not exist
  205.  *            18  no matching directory entry was found
  206.  *            -1  check the critical error flag for critical errors
  207.  */
  208. int  my_findnext( DTA far *dta )
  209. {
  210. void far *old_dta;
  211. void far *new_dta;
  212. int  rc;
  213.  
  214.    new_dta = (void far *)dta;
  215.  
  216.    ASSEMBLE {
  217.  
  218. /*
  219. ; save the old dta
  220. */
  221.         mov     ah, 0x2f                /* DOS get dta */
  222.         int     0x21                    /* DOS interrupt */
  223.         mov     WORD PTR old_dta, bx    /* save OFFSET of old DTA */
  224.         mov     ax, es
  225.         mov     WORD PTR old_dta+2, ax  /* save SEGMENT of old DTA */
  226.  
  227. /*
  228. ; set the new dta
  229. */
  230.         push    ds                      /* save ds */
  231.         mov     dx, WORD PTR new_dta    /* get OFFSET of new dta */
  232.         mov     ax, WORD PTR new_dta+2  /* get SEGMENT of new dta */
  233.         mov     ds, ax                  /* put it in ds */
  234.         mov     ah, 0x1a                /* DOS set dta */
  235.         int     0x21                    /* DOS interrupt */
  236.         pop     ds                      /* get back ds */
  237.  
  238. /*
  239. ; find next matching file
  240. */
  241.         mov     ah, 0x4f                /* DOS find first file */
  242.         int     0x21                    /* DOS interrupt */
  243.  
  244. /*
  245. ; save the return code
  246. */
  247.         jc      an_error                /* carry is set if an error occured */
  248.         xor     ax, ax                  /* zero out ax, return OK if no error */
  249.    }
  250. an_error:
  251.  
  252.    ASSEMBLE {
  253.         mov     WORD PTR rc, ax         /* save the return code */
  254.  
  255. /*
  256. ; get back old dta
  257. */
  258.         push    ds                      /* save ds */
  259.         mov     dx, WORD PTR old_dta    /* get OFFSET of old dta */
  260.         mov     ax, WORD PTR old_dta+2  /* get SEGMENT of old dta */
  261.         mov     ds, ax                  /* put it in ds */
  262.         mov     ah, 0x1a                /* DOS set dta */
  263.         int     0x21                    /* DOS interrupt */
  264.         pop     ds                      /* get back ds */
  265.    }
  266.    if (ceh.flag == ERROR)
  267.       rc = ERROR;
  268.    return( rc );
  269. }
  270.